﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Ricciolo.Controls;
using System.Windows.Controls;
using OpenExpressApp.WPF.Controls;
using OpenExpressApp;
using System.Windows.Data;
using System.Windows.Media;
using OpenExpressApp.Editors;
using OpenExpressApp.MetaModel.Utils;

namespace OpenExpressApp.Module.WPF.Editors.TreeColumn
{
    /// <summary>
    /// 树型表的列的基类
    /// </summary>
    public class TreeColumn
    {
        private BusinessObjectPropertyInfo _propertyInfo;

        public TreeColumn(BusinessObjectPropertyInfo info, IListObjectView view)
        {
            this.PropertyInfo = info;
            this.View = view;
        }

        /// <summary>
        /// 默认没有Editor
        /// </summary>
        public virtual IWPFPropertyEditor Editor
        {
            get
            {
                return null;
            }
        }

        /// <summary>
        /// 正在编辑的属性
        /// </summary>
        public BusinessObjectPropertyInfo PropertyInfo
        {
            get
            {
                return this._propertyInfo;
            }
            set
            {
                this._propertyInfo = value;
                var editor = this.Editor;
                if (editor != null)
                {
                    editor.PropertyInfo = value;
                }
            }
        }

        /// <summary>
        /// 所属的视图
        /// </summary>
        public IListObjectView View { get; private set; }

        /// <summary>
        /// 实际显示的GridViewColumn？？？
        /// </summary>
        public GridViewColumn GridViewColumn { get; set; }

        /// <summary>
        /// 生成一个元素集
        /// 
        /// 默认生成一个有Border的TextBlock
        /// </summary>
        /// <returns></returns>
        public FrameworkElementFactory GenerateFrameworkElementFactory()
        {
            FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border));
            border.SetValue(Border.BorderThicknessProperty, Tree.BorderThickness);
            border.SetValue(Border.BorderBrushProperty, System.Windows.Media.Brushes.Silver);
            border.SetValue(Border.MarginProperty, Tree.ElementMargin);

            FrameworkElementFactory element = this.GenerateElementFactoryInBorder();

            border.AppendChild(element);

            return border;
        }

        /// <summary>
        /// 生成这个Column使用的Element
        /// 默认生成一个TextBlock，并绑定到属性
        /// </summary>
        /// <returns></returns>
        public FrameworkElement GenerateFrameworkElement()
        {
            //生成子类通用的Border
            Border border = new Border()
            {
                BorderThickness = Tree.BorderThickness,
                // SnapsToDevicePixels = true,
                BorderBrush = new SolidColorBrush(Colors.Silver),
                Margin = Tree.ElementMargin,
            };

            border.Child = this.GenerateElementInBorder();
            return border;

            //TextBlock tb = new TextBlock();
            //Binding TextBinding = new Binding(this._propertyInfo.Name);
            //tb.SetBinding(TextBlock.TextProperty, TextBinding);
            //Border border = GenerateBorder();
            //border.Child = tb;
            //return border;
        }

        #region 三个生成确切控件的虚方法

        /// <summary>
        /// 生成这个Column使用的编辑Element
        /// 未实现
        /// </summary>
        /// <returns></returns>
        public virtual FrameworkElement GenerateEditingFrameworkElement()
        {
            var editor = this.Editor;
            if (editor == null) throw new ArgumentNullException("只读！");
            //if (editor.ControlCreated == false)
            //{
            //由于树形可以包含多个对象，一个列可能编辑多个对象，所以现在简单处理为每次重新生成控件
            FrameworkElement control = null;
            if (editor.IsReadOnly)
            {
                control = editor.ReadOnlyControlGenerator.CreateControl();

            }
            else
            {
                control = editor.EditingControlGenerator.CreateControl();
            }

            control.Margin = Tree.ElementMargin;
            //}
            return control;
            //throw new NotImplementedException("未实现GenerateEditingFrameworkElement");
        }

        /// <summary>
        /// 生成一个控件集合，用于显示。
        /// 这个集合在GenerateFrameworkElementFactory方法中会被加上Border显示出来。
        /// </summary>
        /// <returns></returns>
        protected virtual FrameworkElementFactory GenerateElementFactoryInBorder()
        {
            var textBlock = new FrameworkElementFactory(typeof(TextBlock));

            Binding textBinding = new Binding(this._propertyInfo.Name)
            {
                StringFormat = this._propertyInfo.StringFormat
            };
            textBlock.SetBinding(TextBlock.TextProperty, textBinding);

            var propertyType = this._propertyInfo.RuntimeProperty.PropertyType;
            if (TypeHelper.IsNumber(propertyType))
            {
                textBlock.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Right);
            }

            return textBlock;
        }

        /// <summary>
        /// 生成一个控件集合，用于显示。
        /// 这个集合在GenerateFrameworkElement方法中会被加上Border显示出来。
        /// </summary>
        /// <returns></returns>
        protected virtual FrameworkElement GenerateElementInBorder()
        {
            TextBlock textBlock = new TextBlock();

            Binding textBinding = new Binding(this._propertyInfo.Name)
            {
                StringFormat = this._propertyInfo.StringFormat
            };
            textBlock.SetBinding(TextBlock.TextProperty, textBinding);

            var propertyType = this._propertyInfo.RuntimeProperty.PropertyType;
            if (TypeHelper.IsNumber(propertyType))
            {
                textBlock.TextAlignment = TextAlignment.Right;
            }

            return textBlock;
        }

        #endregion
    }
}